import { createRoot, Root } from 'react-dom/client';

const containerMap = new WeakMap<Element | DocumentFragment, Root>();

export function render(node: React.ReactElement, container: Element | DocumentFragment) {
  const root = createRoot(container);
  root.render(node);
  containerMap.set(container, root);
}

export function unmount(container: Element | DocumentFragment) {
  return Promise.resolve().then(() => {
    containerMap.get(container)?.unmount();
    containerMap.delete(container);
  });
}
